//
//  MyPOSlnactiveDirVC.m
//  NewPro
//
//  Created by 何荧红 on 2019/5/13.
//  Copyright © 2019 何荧红. All rights reserved.
//

#import "MyPOSlnactiveDirVC.h"
#import "MyPOSlistVC.h"

#import "FriendStore.h"
#import "BusinessManagerStore.h"
#import "FriendModel.h"
#import "PostlistModel.h"
#import "MyPOSlnactiveDirCell.h"

@interface MyPOSlnactiveDirVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UIView *titleView;
@property (nonatomic, strong) UITextField *Search;
@property (nonatomic, strong) PushView *alert;
@property (nonatomic, strong) UITableView *tableView;
// 数据源数组
@property (nonatomic, strong) NSMutableArray *dataArr;
// 搜索结果数组
@property (nonatomic, strong) NSMutableArray *results;

@property (nonatomic, strong) NSDictionary *datadic;

@end

@implementation MyPOSlnactiveDirVC

- (UIView *)titleView{
    if (!_titleView) {
        _titleView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44)];
        _titleView.backgroundColor = [UIColor clearColor];
        UIButton *settingButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [settingButton setFrame:CGRectMake( -12, 0.0, 44.0, 44.0)];
        [settingButton addTarget:self action:@selector(CancelClik) forControlEvents:UIControlEventTouchUpInside];
        [settingButton setImage:[UIImage imageNamed:@"返回icon"] forState:UIControlStateNormal];
        [_titleView addSubview:settingButton];
        UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake( 36, 5, SCREEN_WIDTH - 96, 34)];
        bgView.layer.masksToBounds = YES;
        bgView.layer.cornerRadius = 17;
        bgView.backgroundColor = [UIColor colorWithWhite:1 alpha:0.3];
        [_titleView addSubview:bgView];
        UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(15, 10, 15, 15)];
        image.image = [UIImage imageNamed:@"白色搜索"];
        [bgView addSubview:image];
        self.Search = [[UITextField alloc]initWithFrame:CGRectMake(37, 0, SCREEN_WIDTH - 96 - 37, 34)];
        self.Search.clearButtonMode = UITextFieldViewModeWhileEditing;
        [self.Search addTarget:self action:@selector(SearchClik:) forControlEvents:UIControlEventEditingChanged];
        self.Search.placeholder = @"畅享合伙人";
        [self.Search setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
        self.Search.font = [UIFont fontWithName:@"PingFang-SC-Medium" size:13];
        self.Search.textColor = [UIColor whiteColor];
        [bgView addSubview:self.Search];
    }
    return _titleView;
}
- (void)SearchClik:(UITextField *)textfield{
    if (textfield.text.length > 0) {
        self.results = [[NSMutableArray alloc]init];
        for (FriendModel *model in self.dataArr) {
            if ([model.realName containsString:textfield.text]) {
                [self.results addObject:model];
            }
        }
        [self.tableView reloadData];
    }else{
        self.results = self.dataArr;
        [self.tableView reloadData];
    }
}
- (NSMutableArray *)results{
    if (!_results) {
        _results = [[NSMutableArray alloc]init];
    }
    return _results;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initCSS];
    [self performSelectorOnMainThread:@selector(refresh) withObject:nil waitUntilDone:0.1f];
}
- (void)refresh{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    MJWeakSelf
    [[[FriendStore alloc] init] getFriendListWithCustomId:[UserDefaultsManager sharedInstance].user_id andType:@"1" andKey:@"" andPx:@"0" Success:^(NSArray *listArr, NSString *headImg,NSString *name,NSString *leve) {
        [MBProgressHUD hideHUDForView:weakSelf.view animated:YES];
        weakSelf.dataArr = [FriendModel mj_objectArrayWithKeyValuesArray:listArr];
        weakSelf.results = [FriendModel mj_objectArrayWithKeyValuesArray:listArr];
        [weakSelf.tableView reloadData];
        [weakSelf.tableView.mj_header endRefreshing];
    } Failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:weakSelf.view animated:YES];
        [weakSelf showMBPError:[HttpTool handleError:error]];
        [weakSelf.tableView.mj_header endRefreshing];
    }];
}
- (void)setNavBar{
    self.view.backgroundColor = [UIColor colorWithHexString:@"F1F1F1"];
    self.navigationItem.hidesBackButton = YES;
    self.navigationItem.titleView = self.titleView;
}
- (void)initCSS{
    [self setNavBar];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor clearColor];
    [self.tableView registerNib:[UINib nibWithNibName:@"MyPOSlnactiveDirCell" bundle:nil] forCellReuseIdentifier:@"MyPOSlnactiveDirCellID"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.sectionFooterHeight = 0.01f;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.showsHorizontalScrollIndicator = NO;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
}
- (void)CancelClik{
    [self.navigationController popViewControllerAnimated:YES];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.results.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 63;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 29;
    }
    return 0.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        UIView *header = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 29)];
        header.backgroundColor = [UIColor clearColor];
        UILabel *lab = [[UILabel alloc]init];
        lab.textColor = [UIColor colorWithHexString:@"999999"];
        lab.font = [UIFont fontWithName:@"PingFang-SC-Medium" size:11];
        lab.text = [NSString stringWithFormat:@"合伙人 共%ld人",self.results.count];
        [header addSubview:lab];
        [lab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(header).offset(14);
            make.centerY.equalTo(header);
        }];
        return header;
    }
    return nil;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MyPOSlnactiveDirCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyPOSlnactiveDirCellID"];
    if (self.results.count > 0) {
        cell.model = self.results[indexPath.section];
        if (indexPath.section < self.results.count - 1) {
            cell.line.hidden = NO;
        }else{
            cell.line.hidden = YES;
        }
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    WS(weakself);
    FriendModel *model = self.results[indexPath.section];
    self.alert = [[PushView alloc]initWithAlertView:@"划拨至合伙人" info:model.realName type:0];
    [_alert setAlertsureBlock:^{
        [weakself tonextdata:model];
    }];
    [self.view insertSubview:_alert aboveSubview:self.navigationController.navigationBar];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [self.Search resignFirstResponder];
}
- (void)tonextdata:(FriendModel *)model{
    NSMutableArray *subArr = [[NSMutableArray alloc]init];
    if (self.infoArr.count > 0) {
        for (PostlistModel *model in self.infoArr) {
            [subArr addObject:model.sn];
        }
    }
    [[[BusinessManagerStore alloc]init] PostPOSFlowWithPrams:@{@"fromId":[UserDefaultsManager sharedInstance].user_id,@"toId":model.uid,@"snList":subArr,@"backAmount":self.minmoney} Success:^(NSDictionary * _Nonnull res) {
        if ([res[@"msg"] isEqualToString:@"success"]) {
            [self CleanSuccessAlert];
        }else{
            [self showMBPError:res[@"msg"]];
        }
        [self.alert removeFromSuperview];
    } Failure:^(NSError * _Nonnull error) {
        [self showMBPError:[HttpTool handleError:error]];
    }];
}
- (void)CleanSuccessAlert{
    UIViewController *subview = [self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count - 2];
    MyPOSlistVC *VC = nil;
    if ([subview isKindOfClass:[MyPOSlistVC class]]) {
        VC = (MyPOSlistVC *)subview;
        [VC FristReloadData];
    }
    PushView *success = [[PushView alloc]initWithSuccessAlertView];
    [success setAlertsureBlock:^{
        if (VC) {
            [[AppDelegate sharedAppDelegate] popToViewController:VC];
        }
    }];
    [self.view insertSubview:success aboveSubview:self.navigationController.navigationBar];
}

@end
